package cn.yx.common.mybatis.handler;

import cn.yx.common.core.util.date.DateUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>MyBatis-plus数据自动填充</p>
 *
 * @author Wgssmart
 */
@Component
public class DataMetaObjectHandler implements MetaObjectHandler {

    private static final String PROPERTY_CREATE_TIME = "createTime";
    private static final String PROPERTY_UPDATE_TIME = "updateTime";

    /**
     * 插入数据时，自动填充createTime
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, PROPERTY_CREATE_TIME, DateUtil::date, Date.class);
    }

    /**
     * 更新数据时，自动填充updateTime
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, PROPERTY_UPDATE_TIME, DateUtil::date, Date.class);
    }

}
